/*
 * @Author: WHAlex
 * @Date: 2025-01-18 15:18:55
 *
 * Copyright (c) 2025 by atguigu, All Rights Reserved.
 */
#include "App_MQTT.h"

static const char *TAG = "smart_door_bell";

extern TaskHandle_t main_task_handler;

/**
 * @description: 接收到数据之后的分析处理
 * @return {*}
 */
static void App_MQTT_Data_Handler(esp_mqtt_event_handle_t event)
{
    // 取出数据 {"cmd":"on","type":"audio","dir":"client2esp"}
    char *data = event->data;

    // 将字符串数据转换为JSON对象
    cJSON *obj = cJSON_Parse(data);

    // 取出JSON数据中的键值对(Item)
    cJSON *cmd = cJSON_GetObjectItem(obj, "cmd");   //{"cmd":"on"}
    cJSON *type = cJSON_GetObjectItem(obj, "type"); //{"type":"audio"}
    cJSON *dir = cJSON_GetObjectItem(obj, "dir");   //{"dir":"client2esp"}

    // 取出单个键值对中的Value
    char *cmdStr = cJSON_GetStringValue(cmd);   // on or off
    char *typeStr = cJSON_GetStringValue(type); // audio or video
    char *dirStr = cJSON_GetStringValue(dir);   // client2esp or esp2client

    // 根据具体指令组合通知到主任务做相应处理
    uint32_t action = ACTION_AUDIO_VIDEO_NO;
    if (strcmp(typeStr, "audio") == 0)
    {
        if (strcmp(cmdStr, "on") == 0)
        {
            if (strcmp(dirStr, "client2esp") == 0)
            {
                action = ACTION_AUDIO_CLIENT2ESP_ON;
            }
            else if (strcmp(dirStr, "esp2client") == 0)
            {
                action = ACTION_AUDIO_ESP2CLIENT_ON;
            }
        }
        else if (strcmp(cmdStr, "off") == 0)
        {
            if (strcmp(dirStr, "client2esp") == 0)
            {
                action = ACTION_AUDIO_CLIENT2ESP_OFF;
            }
            else if (strcmp(dirStr, "esp2client") == 0)
            {
                action = ACTION_AUDIO_ESP2CLIENT_OFF;
            }
        }
    }
    else if (strcmp(typeStr, "video") == 0)
    {
        if (strcmp(cmdStr, "on") == 0)
        {
            action = ACTION_VIDEO_ESP2CLIENT_ON;
        }
        else if (strcmp(cmdStr, "off") == 0)
        {
            action = ACTION_VIDEO_ESP2CLIENT_OFF;
        }
    }
    xTaskNotify(main_task_handler, action, eSetValueWithOverwrite);
}

static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
    ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id);
    esp_mqtt_event_handle_t event = event_data;
    esp_mqtt_client_handle_t client = event->client;
    switch ((esp_mqtt_event_id_t)event_id)
    {
    case MQTT_EVENT_CONNECTED:
        esp_mqtt_client_subscribe(client, "teacher/cmd", 0);
        ESP_LOGI(TAG, "发送订阅主题请求...");
        break;
    case MQTT_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
        break;
    case MQTT_EVENT_SUBSCRIBED:
        ESP_LOGI(TAG, "订阅主题成功...");
        break;
    case MQTT_EVENT_UNSUBSCRIBED:
        break;
    case MQTT_EVENT_PUBLISHED:
        break;
    case MQTT_EVENT_DATA:
        // printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
        // printf("DATA=%.*s\r\n", event->data_len, event->data);
        App_MQTT_Data_Handler(event);
        break;
    case MQTT_EVENT_ERROR:
        ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
        break;
    default:
        ESP_LOGI(TAG, "Other event id:%d", event->event_id);
        break;
    }
}

/**
 * @description: 创建MQTT客户端
 * @return {*}
 */
void App_MQTT_Start(void)
{
    // MQTT客户端配置信息,配置服务器地址
    const esp_mqtt_client_config_t mqtt_cfg = {
        .broker.address.uri = "ws://broker.emqx.io:8083/mqtt",
    };

    // MQTT客户端初始化
    esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);

    // 注册回调函数
    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);

    // 启动客户端
    esp_mqtt_client_start(client);
}